package com.banzhi.shop.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banzhi.shop.R;
import com.banzhi.shop.adapter.CommonAdapter;
import com.banzhi.shop.adapter.CommonHeadAdapter;
import com.banzhi.shop.adapter.GroupingProfitAdapter;
import com.banzhi.shop.adapter.MyShopAdapter;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.data.GroupingProfitData;
import com.banzhi.shop.fragment.Grouping_DaifaFragment;
import com.banzhi.shop.fragment.Grouping_YifaFragment;
import com.banzhi.shop.model.CallBack.GroupingProfitCallback;
import com.banzhi.shop.model.Impl.GroupingProfitImpl;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.EndlessRecyclerOnScrollListener;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.NumberUtils;
import com.banzhi.shop.utils.ShareUtils;
import com.banzhi.shop.utils.TabLayoutUtil;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;

import com.squareup.picasso.Picasso;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.banzhi.shop.MyApp.context;

//拼团收益
public class GroupingProfitActivity extends BaseActivity implements View.OnClickListener, GroupingProfitCallback, SwipeRefreshLayout.OnRefreshListener {

    private ImageView mIvBack;
    private TextView mTvGroupingGoods;
    private TextView mTvGroupingProfit;
    private LinearLayout mLlGoods;
    private SwipeRefreshLayout mSmartRefreshLayout;
    private RecyclerView mRecyclerView;
    private LinearLayout mLlProfit;
    private TabLayout mTabs;
    private ViewPager mViewPager;

    private List<String> mTitle2 = new ArrayList<>();
    private List<Fragment> mFragment2 = new ArrayList<>();

    private GroupingProfitAdapter mAdapter;
    private GroupingProfitImpl model;
    private int page = 1;
    private GroupingProfitData mGroupingProfitData;
    private GroupingProfitData.GroupingProfitBean mGroupingProfitBean;
    private List<GroupingProfitData.GroupingProfitBean.DataBean> data = new ArrayList<>();
    private List<GroupingProfitData.GroupingProfitBean.DataBean> mDataBeanList;
    private CommonHeadAdapter<GroupingProfitData.GroupingProfitBean.DataBean> adapter;
    private LinearLayoutManager mLayoutManager;
    private TextView mTotal;
    private TextView mTotalPrice;
    private TextView mTvTotal;
    private TextView mTvTotalProfit;
    private RelativeLayout mRlnodata;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grouping_profit2);
        Translucent.setTranslucent(this);
        initView();
        setListener();
        model = new GroupingProfitImpl();
        LoadingDialogUtils.showProgress(GroupingProfitActivity.this, "正在加载,请稍后~");
        model.getData(page + "", "2", null, this);

        mAdapter = new GroupingProfitAdapter(getSupportFragmentManager(), mTitle2, mFragment2);
        mViewPager.setAdapter(mAdapter);
        //为TabLayout设置ViewPager
        mTabs.setupWithViewPager(mViewPager);
        //使用ViewPager的适配器
        mTabs.setTabsFromPagerAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
        mTabs.post(new Runnable() {
            @Override
            public void run() {
                TabLayoutUtil.setIndicator(mTabs, 20, 20);
            }
        });
    }

    private void initView() {
        mIvBack = (ImageView) findViewById(R.id.iv_back);
        mTvGroupingGoods = (TextView) findViewById(R.id.tv_grouping_goods);
        mTvGroupingProfit = (TextView) findViewById(R.id.tv_grouping_profit);
        mLlGoods = (LinearLayout) findViewById(R.id.ll_goods);
        mSmartRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.smart_refresh_layout);
        mSmartRefreshLayout.setColorSchemeResources(R.color.themeColor);
        mSmartRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.colorWhite);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mLlProfit = (LinearLayout) findViewById(R.id.ll_profit);
        mTabs = (TabLayout) findViewById(R.id.tabs);
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mTotal = (TextView) findViewById(R.id.tv_total1);
        mTotalPrice = (TextView) findViewById(R.id.tv_total_profit2);
        mTvTotal = (TextView) findViewById(R.id.tv_total);
        mTvTotalProfit = (TextView) findViewById(R.id.tv_total_profit);
        mRlnodata = (RelativeLayout) findViewById(R.id.rl_nodata_goods);

        mTitle2.add("待发放");
        mTitle2.add("已发放");

        mFragment2.add(new Grouping_DaifaFragment());
        mFragment2.add(new Grouping_YifaFragment());
    }

    private void setListener() {
        mIvBack.setOnClickListener(this);
        mTvGroupingGoods.setOnClickListener(this);
        mTvGroupingProfit.setOnClickListener(this);
        mSmartRefreshLayout.setOnRefreshListener(this);
        mRecyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(mLayoutManager) {
            @Override
            public void onLoadMore() {
                page++;
                LoadingDialogUtils.showProgress(GroupingProfitActivity.this, "正在加载,请稍后~");
                model.getData(page + "", "2", null, GroupingProfitActivity.this);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_grouping_goods:
                mTvGroupingProfit.setBackgroundResource(R.mipmap.bg2);
                mTvGroupingProfit.setTextColor(Color.WHITE);
                mLlProfit.setVisibility(View.GONE);
                mLlGoods.setVisibility(View.VISIBLE);
                mTvGroupingGoods.setBackgroundResource(R.mipmap.bg1);
                mTvGroupingGoods.setTextColor(ContextCompat.getColor(this, R.color.themeColor));
                break;

            case R.id.tv_grouping_profit:
                mTvGroupingProfit.setBackgroundResource(R.mipmap.bg5);
                mTvGroupingProfit.setTextColor(ContextCompat.getColor(this, R.color.themeColor));
                mLlGoods.setVisibility(View.GONE);
                mLlProfit.setVisibility(View.VISIBLE);
                mTvGroupingGoods.setBackgroundResource(R.mipmap.bg4);
                mTvGroupingGoods.setTextColor(Color.WHITE);
                break;

        }

    }

    @Override
    public void ongetDataSuccess(Object object) {
        mGroupingProfitData = (GroupingProfitData) object;
        mDataBeanList = mGroupingProfitData.getData().getData();
        if (page == 1) {
            this.data.clear();
        } else {
            if (mDataBeanList.size() == 0) {
//                ToastUtils.makeToastShort("没有更多了");
                return;
            }
        }
        data.addAll(mDataBeanList);
     //   LogUtils.e("GroupingProfit1",data.size()+"");
        if (data.size() == 0) {
            mRlnodata.setVisibility(View.VISIBLE);

        } else {
            mRlnodata.setVisibility(View.GONE);
        }
        showView();
        if (adapter == null) {
            adapter = new CommonHeadAdapter<GroupingProfitData.GroupingProfitBean.DataBean>(this, R.layout.recyclerview_item_grouping_goods, data) {
                @Override
                public void convert(ViewHolder holder, final GroupingProfitData.GroupingProfitBean.DataBean dataBean, int position) {
                    holder.setText(R.id.tv_option, dataBean.getOption());
                 /*   holder.getTextView(R.id.tv_fuzhi).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ShareUtils.copyLianjie(GroupingProfitActivity.this, dataBean.getOrderno());
                        }
                    });*/
                    holder.setText(R.id.tv_title, dataBean.getTitle());
                    if (ASUtils.isCanUse(dataBean.getGroupsprice())) {
                        holder.setText(R.id.tv_groupsprice, NumberUtils.DecimalFormat(dataBean.getGroupsprice()));
                    }
                    if (ASUtils.isCanUse(dataBean.getGroupsprice())) {
                        holder.setText(R.id.tv_groupsprice1, NumberUtils.DecimalFormat(dataBean.getGroupsprice()));
                    }
                    if (ASUtils.isCanUse(dataBean.getCreatetime())) {
                        holder.setText(R.id.tv_time, dataBean.getCreatetime());
                    }
                    Picasso.with(context).load(dataBean.getThumb()).into(holder.getImageView(R.id.iv_thumb));
                  //  holder.setText(R.id.tv_statu, dataBean.getStatus_value());
                    holder.getTextView(R.id.button1).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(GroupingProfitActivity.this, GroupOrderDetailsActivity.class);
                            intent.putExtra("goodid",dataBean.getGoodid());
                            intent.putExtra("teamid",dataBean.getTeamid());
                            startActivity(intent);
                        }
                    });


                }

                @Override
                public void headconvert(HeaderViewHolder holder) {

                }
            };
            adapter.setOnItemClickListener(new CommonHeadAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    if ("1".equals(data.get(position).getGoodstatus())) {
                        Intent intent1 = new Intent(GroupingProfitActivity.this, GroupingDetailsActivity.class);
                        intent1.putExtra("id",data.get(position).getGid());
                        startActivity(intent1);
                    }else {
                        ToastUtils.makeToastShort("该拼团商品已经下架。");
                    }
                }
            });


            mRecyclerView.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
        if (mSmartRefreshLayout.isRefreshing()) {
            mSmartRefreshLayout.setRefreshing(false);
        }
    }

    private void showView() {
        mTotal.setText(mGroupingProfitData.getData().getTotal().getTotal());
        mTotalPrice.setText(mGroupingProfitData.getData().getTotal().getTotalprice());
        mTvTotalProfit.setText(mGroupingProfitData.getData().getTotal().getTotalprofit());
        mTvTotal.setText(mGroupingProfitData.getData().getTotal().getTotal());
    }


    @Override
    public void ongetDataError(Object object) {
        if (mSmartRefreshLayout.isRefreshing()) {
            mSmartRefreshLayout.setRefreshing(false);
        }
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(GroupingProfitActivity.this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }


    @Override
    public void onRefresh() {
        page = 1;
        LoadingDialogUtils.showProgress(GroupingProfitActivity.this, "正在加载,请稍后~");
        model.getData(page + "", "2", null, this);
    }
}
